package com.waypolice.manager.controller;

import com.waypolice.base.BaseConstants;
import com.waypolice.common.constant.BorrowConstant;
import com.waypolice.common.constant.CmsResult;
import com.waypolice.common.utils.MathUtils;
import com.waypolice.manager.service.BorRegisterService;
import com.waypolice.manager.service.CmsSinItemService;
import com.waypolice.manager.service.VolAndItemService;
import com.waypolice.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author zyp
 * @description 借阅归还确认控制器
 * @create 2019-03-20 18:09
 */
@Controller
@RequestMapping(value = "/pcwindow")
@Api(value = "归还确认", description = "归还确认管理")
public class BorrowReturnAction {

    private static final Logger logger = LoggerFactory.getLogger(BorrowReturnAction.class);

    @Autowired
    private BorRegisterService borRegisterService;

    @Autowired
    private VolAndItemService volAndItemService;

    @Autowired
    private CmsSinItemService cmsSinItemService;

    /**
     * @author:zyp
     * @description 跳转归还确认页面
     * @date: 2019/3/20 18:11
     * @return
     */
    @ApiOperation(value = "跳转归还确认页面")
    @RequestMapping(value = "/toBorrowReturn")
//    @RequiresPermissions("cms:borrowreturn:read")
    public String jumpToBorrowPage(){
        return "/borrowManager/borrowReturn";
    }

    /**
     * @author:zyp
     * @description 获取线下借阅的登记记录
     * @date: 2019/3/20 18:22
     * @return
     */
    @ApiOperation(value = "获取线下借阅的登记记录")
    @RequestMapping(value = "/borReturnList")
    @RequiresPermissions("cms:borrowreturn:read")
    @ResponseBody
    public Object getBorrowReturnList(@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
                                      @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
                                      @RequestParam(required = false, value = "sort") String sort,
                                      @RequestParam(required = false, value = "order") String order,String fileType){
        Map<String, Object> result = borRegisterService.getBorrowReturnList(offset, limit, sort, order, fileType);
        return result;
    }

    /**
     * @author:zyp
     * @description 档案借出确认
     * @date: 2019/3/21 10:17
     * @return 
     */
    @ApiOperation(value = "档案借出确认")
    @RequestMapping(value = "/beSureBorrow")
//    @RequiresPermissions("cms:borrowreturn:read")
    @ResponseBody
    public Object beSureBorrow(String ids){
        if (StringUtils.isBlank(ids)) {
            return new CmsResult(0,"解析选中的文件错误！",0);
        }else {
            String[] idArray = ids.split("-");
            for (String idStr : idArray) {
                CmsBorRegister cmsBorRegister = new CmsBorRegister();
                cmsBorRegister.setId(Long.parseLong(idStr));
                cmsBorRegister.setBorStatus(BorrowConstant.BORROW_STATUS_BOR);
                borRegisterService.updateByPrimaryKeySelective(cmsBorRegister);
            }
        }
        return new CmsResult(1,"确认成功！",0);
    }


    /**
     * @author:zyp
     * @description 借阅归还确认
     * @date: 2019/3/21 10:20
     * @return 
     */
    @ApiOperation(value = "借阅归还确认")
    @RequestMapping(value = "/makeSureReturn")
//    @RequiresPermissions("cms:borrowreturn:read")
    @ResponseBody
    public Object makeSureReturn(String ids,String fileType){
        String flag="0";
        if (StringUtils.isBlank(ids)) {
            return new CmsResult(0,"解析选中的文件错误！",0);
        }else {
            String[] idArray = ids.split("-");
            for (String idStr : idArray) {
                //根据当前的登记号id 从 CMS_BOR_REG_FILE【登记表与文件关联表】表中获取文件形式 以及 文件id
                List<CmsBorRegFile> cmsBorRegFiles = borRegisterService.selectBorRgtistById(Long.parseLong(idStr));
                //防止修改卷次数过多问题
                boolean isUpdateVolum = false;
                for (CmsBorRegFile cmsBorRegFile : cmsBorRegFiles ){
                    //文件id
                    Long fileId = cmsBorRegFile.getFileId();
                    //文件形式 卷/件
                    String fileShape = cmsBorRegFile.getFileShape();
                    //如果当前保存的形式为卷
                    if(BorrowConstant.BORROW_FILE_VOLUM==MathUtils.string2long(fileShape)){
                        CmsJudVolume cmsJudVolume = new CmsJudVolume();
                        cmsJudVolume.setVolId(fileId);
                        cmsJudVolume.setVolStatus(0);
                        volAndItemService.updateByPrimaryKeySelectiveNoCheck(cmsJudVolume);
                        //同时修改件的状态
                        volAndItemService.updateItemStatusByVoId(0,cmsBorRegFile.getFileId());
                    }else if (BorrowConstant.BORROW_FILE_ITEM==MathUtils.string2long(fileShape)){
                        CmsJudItem cmsJudItem = new CmsJudItem();
                        cmsJudItem.setItemId(fileId);
                        cmsJudItem.setItemStatus(0L);
                        volAndItemService.updateItemByPrimaryKeySelective(cmsJudItem);
                        cmsJudItem = volAndItemService.selectByItemId(fileId);
                        //判断当前借阅的件的数量与卷包含的数量是否一致
                        //当借阅的件的数量与卷包含的数量一致时，同时需要修改卷的状态
                        Long itmeVolId = cmsJudItem.getItmeVolId();
                        CmsJudVolume cmsJudVolume = volAndItemService.selectCmsJudVolumeByVolId(itmeVolId);
                        Long voId = cmsJudVolume.getVolId();
                        List<CmsJudItem> cmsJudItems = volAndItemService.selectAllByVolId(voId, 0);
                        //当借阅的件的数量与卷包含的数量一致时，同时需要修改卷的状态
                        if(cmsJudItems.size()==cmsBorRegFiles.size()&&!isUpdateVolum){
                            cmsJudVolume.setVolStatus(2);
                            volAndItemService.updateByPrimaryKeySelectiveNoCheck(cmsJudVolume);
                            isUpdateVolum = true;
                        }
                    }else if(BorrowConstant.BORROW_FILE_SIN_ITEM==MathUtils.string2long(fileShape)){
                        CmsSinItem cmsSinItem = new CmsSinItem();
                        cmsSinItem.setItemId(cmsBorRegFile.getFileId());
                        cmsSinItem.setItemStatus(0L);
                        cmsSinItemService.updateSinItemByPrimaryKeySelective(cmsSinItem);
                    }
                }
                //初始状态
                CmsBorRegister cmsBorRegister = new CmsBorRegister();
                cmsBorRegister.setId(Long.parseLong(idStr));
                cmsBorRegister.setBorStatus(3);
                cmsBorRegister.setReturnTime(new Date());
                borRegisterService.updateByPrimaryKeySelective(cmsBorRegister);
            }
        }
        return new CmsResult(1,"确认归还成功！",0);
    }

}